<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>系统常态化运行评分系统</title>
    <link rel="stylesheet" href="../component/pear/css/pear.css" />
    <style>
        .expand.pear-btn:hover {
            color: currentColor;
            background: none;
        }

        .expand.pear-btn {
            border: 1px solid rgba(255, 255, 255, 0)
        }

        .layui-table-tool-temp {
            display: flex;
            justify-content: flex-start;
            padding-right: 0;
        }
        .layui-table-box button {
            font-size: 12.5px !important;
            float: left;
            margin-right: 5px; /* 根据需要调整间距 */
        }
    </style>
</head>

<body class="pear-container mymine">
    <div class="layui-card">
        <div id="divCardHeader" class="layui-card-header">

        </div>
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>


    <script type="text/html" id="user-toolbar">
        {{# if (localStorage.getItem('roleType') == 4&& localStorage.getItem('isManager') =='true') { }}
            <button class="pear-btn pear-btn-primary pear-btn-md"  lay-event="addPf">
                <i class="layui-icon layui-icon-addition"></i>
                新增评分任务
            </button>
     {{# } else{ $('.layui-table-tool').hide();$('#divCardHeader').hide(); } }}
   
</script>




    <div style="display: none;">
        <div class="layer-top">
            <div class="layui-card">
                <div class="layui-card-header" id="divHeader">新增评分任务</div>
            </div>
            <div class="layui-card" style="margin-top: 10px;">
                <form class="layui-form" action="" onsubmit="return false" lay-filter="lay-info">
                    <input type="hidden" name="id" />
                    <div class="layui-form-item">
                        <label class="layui-form-label">任务名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="taskName" required lay-verify="required" placeholder="请输入任务名称"
                                autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">评分日期</label>
                        <div class="layui-input-block">
                            <input type="text" id="checkTime" name="checkTime" lay-verify="date"
                                placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button id="btn" class="pear-btn pear-btn-md pear-btn-primary" lay-submit
                                lay-filter="lay-user-add">新增评分任务</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <script src="../component/layui/layui.js"></script>
    <script src="../component/pear/pear.js"></script>
    <script src="../scripts/utils/request.js"></script>
    <script src="../scripts/utils/auth.js"></script>
    <script>
        var $, table, form, drawer, popup, notice, laydate, dropdown, upload
        layui.use(['table', 'form', 'jquery', 'drawer', 'popup', 'notice', 'laydate', 'dropdown', 'upload'], function () {
            table = layui.table;
            form = layui.form;
            $ = layui.jquery;
            drawer = layui.drawer;
            popup = layui.popup;
            notice = layui.notice;
            laydate = layui.laydate;
            dropdown = layui.dropdown;
            upload = layui.upload;


            $("#divCardHeader").html(localStorage.getItem('mineName')+"评分记录查询")
            var iskQ = localStorage.getItem('roleType') == 3
            var minId = getUrlParams('id');

            laydate.render({
                elem: '#checkTime'
            });

            table.render({
                id: "user-table",
                elem: '#user-table',
                url: baseURL + '/api/pf/list',
                method: 'post',
                contentType: 'application/json',
                page: true,
                loading: true,
                headers: {
                    token: localStorage.getItem("token")
                },
                request: {
                    pageName: 'pageIndex', // page
                    limitName: 'pageSize', // limit
                },
                where: {
                    mineId: iskQ ? minId : ''
                },
                cols: [[
                    { field: 'checkTime', align:'center', width: 100, title: '评分日期' },
                    { field: 'taskName',align:'center', minWidth: 230, title: '任务名称' },
                    {
                        field: 'nCount',align:'center', minWidth: 180, title: '系统总数量 /自评', templet: function (d) {
                            if (d.total === d.commitTotal) {
                                return "<span style='color: green'>" + d.total + " / " + d.commitZpTotal +  "</span>";
                            }
                            return d.total + ' / ' + d.commitZpTotal ;
                        }
                    },
                    { field: 'pfScore', align:'center',width: 100, title: '自评得分' },
                    {
                        field: '任务状态',align:'center', width: 140, title: '任务状态', templet: function (d) {
                            console.log(d.taskType, '*****')

                            if (d.state == 1) {
                                return "<span style='color: #ff5722'>待自评</span>";
                            } else if (d.state == 2 && d.taskType == 2) {
                                return "<span style='color: #ffb800' >待核验</span>";
                            } else if (d.state == 2 && d.taskType == 1) {
                                return "<span style='color: #ffb800' >待上传整改计划</span>";
                            } else if (d.state == 3) {
                                return "<span style='color: #31bdec' title=''>待上传整改计划</span>";
                            } else if (d.state == 4) {
                                return "<span style='color:#16b777' title=''>已完成</span>";
                            }
                        }
                    },

                    {
                        title: '操作', 
                        fixed:'right',
                        templet: function (d) {
                            var buttons = '';
                            var iskQ = localStorage.getItem('roleType') == 3;

                            if (!iskQ) {
                                if (d.state == 1) {
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>自评</button>';
                                    if(localStorage.getItem('isManager')=='true'){
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="modify"><i class="layui-icon layui-icon-edit"></i>修改</button>';
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</button>';
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-addition"></i>复制</button>';
                                    }
                                } else if (d.state == 2) {
                                    if (d.taskType == 1) {
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                        if(localStorage.getItem('isManager')=='true'){
                                            buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="modify"><i class="layui-icon layui-icon-edit"></i>修改</button>';
                                            buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-addition"></i>复制</button>';
                                            buttons += '<button id="uploadBtn" class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="upload" title="上传整改计划"><i class="layui-icon layui-icon-upload"></i>上传</button>';
                                        }
                                    } else if (d.taskType == 2) {
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                        if(localStorage.getItem('isManager')=='true'){
                                            buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="modify"><i class="layui-icon layui-icon-edit"></i>修改</button>';
                                            buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-addition"></i>复制</button>';
                                        }
                                    }
                                } else if (d.state == 3) {
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                    if(localStorage.getItem('isManager')=='true'){
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-addition"></i>复制</button>';
                                        buttons += '<button id="uploadBtn" class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="upload" title="上传整改计划"><i class="layui-icon layui-icon-upload"></i>上传</button>';
                                    }
                                }else if (d.state == 4) {
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                    buttons += '<button id="uploadBtn" class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadzgjh" title="下载整改计划"><i class="layui-icon layui-icon-download-circle"></i>下载</button>';
                                    if(localStorage.getItem('isManager')=='true'){
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-addition"></i>复制</button>';
                                    }
                                }
                            } else {
                                if (d.state == 2) {
                                    if (d.taskType == 1) {
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                    } else if (d.taskType == 2) {
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>核验</button>';
                                        buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                    }
                                } else if (d.state == 3) {
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                } else if (d.state == 4) {
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="pf"><i class="layui-icon layui-icon-search"></i>查看</button>';
                                    buttons += '<button class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadReports" title="导出评分结果"><i class="layui-icon layui-icon-export"></i>导出</button>';
                                    buttons += '<button id="uploadBtn" class="pear-btn pear-btn-md pear-btn-primary pear-border-blue pear-btn-xs" lay-event="downloadzgjh" title="下载整改计划"><i class="layui-icon layui-icon-download-circle"></i>下载</button>';
                                }
                            }

                            return buttons;
                        }, minWidth: 350, align: 'center'
                    }
                ]],
                skin: 'grid',
                toolbar: '#user-toolbar',
                defaultToolbar: [],
                done: function (res, curr, count) {
                    validToken(res);
                }
            });
            var bottonState = ''
            table.on('tool(user-table)', function (obj) {
                var data = obj.data; // 获得当前行数据
                if (localStorage.getItem('roleType') == 3) {
                    parent = parent.parent
                }
                // console.log(obj)
                if (obj.event === 'pf') {
                    //页面跳转
                    const id = obj.data.id;
                    const name = obj.data.taskName;
                    const taskType = obj.data.taskType;
                   
                    parent.layui.admin.addTab('pfSystem'  + id, name + "-系统列表", `./page/pfsystemlist.html?id=${id}&taskType=${taskType}`)
                    parent.layui.admin.refresh('pfSystem' + id);

                } else if (obj.event === 'copy') {
                    bottonState = 'copy';
                    pfId = obj.data.id;
                    //修改
                    form.val('lay-info', {
                        "id": obj.data.id,
                        "taskName": "",
                        "checkTime": null,
                        "taskType": ""
                    });
                    $("#btn").text("复制评分任务");
                    $("#divHeader").html("复制评分任务");
                    drawer.open({
                        direction: "right",
                        dom: ".layer-top",
                        distance: "40%"
                    });
                } else if (obj.event === 'upload') {
                    // 创建一个隐藏的文件选择输入框
                    var uploadButton = $('<input type="file" accept="*"/>');
                    // 在选择文件后触发上传
                    uploadButton.on('change', function (e) {
                        var file = e.target.files[0];
                        if (!file) return;

                        var formData = new FormData();
                        formData.append("file", file);

                        formData.append("taskId", data.id); // 添加任务 ID

                        // 使用 jQuery 的 ajax 方法上传文件
                        $.ajax({
                            url: baseURL + '/api/file/uploadMinePf?taskId=' + data.id,
                            type: 'POST',
                            data: formData,
                            headers: {
                                token: localStorage.getItem("token") // 将 token 添加到请求头
                            },
                            processData: false, // 不处理数据
                            contentType: false, // 不设置内容类型
                            success: function (res) {
                                if (res.code === 0) {
                                    // 文件上传成功后的处理
                                    postAjax('/api/pf/uploadZgjh', {
                                        pfId: data.id,
                                        zgjhUrl: res.data
                                    }, function (response) {
                                        table.reload('user-table');
                                    }, null);
                                } else {
                                    layer.msg('上传失败：' + res.message);
                                }
                            },
                            error: function () {
                                layer.msg('上传时发生错误');
                            }
                        });
                    });
                    // 触发文件选择
                    uploadButton.click();
                } else if (obj.event === 'delete') {
                    delPf(obj);
                } else if (obj.event === 'modify') {
                    bottonState = 'modify';
                    // 修改操作
                    form.val('lay-info', {
                        "id": data.id,
                        "taskName": data.taskName,
                        "checkTime": data.checkTime,
                        "checkTeam": data.checkTeam
                    });
                    $("#btn").text("修改评分任务");
                    $("#divHeader").html("修改评分任务");
                    drawer.open({
                        direction: "right",
                        dom: ".layer-top",
                        distance: "40%"
                    });
                } else if (obj.event === 'downloadzgjh') {
                    if(data.zgjhUrl==null|| data.zgjhUrl===''){
                        notice.showTips('未上传整改计划文件!');
                    }
                    postAjaxNoSuccessTip('/api/file/previewFilePf',{'id':data.zgjhUrl},function(response){
                        window.open(response.data);
                    });
                } else if (obj.event === 'downloadReports') {
                    parent.window.open(baseURL + '/api/report/exportPfById?id=' + data.id + '&token=' + localStorage.getItem('token'));
                }
            });
           
            /**
             * 新增操作
             */
            table.on('toolbar(user-table)', function (obj) {
                if (obj.event === 'addPf') {
                    window.addPf();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                }
            });

            form.on('submit(user-query)', function (data) {
                table.reload('user-table', {
                    where: data.field,
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                })
                return false;
            });


            /**
             * 新增/修改评分
             */
            form.on('submit(lay-user-add)', function (data) {
                let reqData = data.field;
                reqData.taskType=1;
                if (!reqData.id || reqData.id == '') {
                    postAjax('/api/pf/add', reqData, function (response) {
                        drawer.closeAll();
                        table.reload('user-table');
                        bottonState = '';
                    }, null);
                } else if (bottonState == 'modify') {
                    postAjax('/api/pf/modify', reqData, function (response) {
                        drawer.closeAll();
                        table.reload('user-table');
                        bottonState = '';
                    }, null);
                } else if (bottonState == 'copy') {
                    reqData.pfId = reqData.id;
                    delete reqData.id;
                    postAjax('/api/pf/copyPfTask', reqData, function (response) {
                        drawer.closeAll();
                        table.reload('user-table');
                        bottonState = '';
                    }, null);
                }

            });


            window.refresh = function (param) {
                table.reload('user-table');
            };

            /**
             * 新增评分任务
             */
            window.addPf = function () {
                form.val('lay-info', {
                    "id": "",
                    "taskName": "",
                    "checkTime": null,
                    "taskType": ""
                });
                $("#btn").text("新增评分任务");
                $("#divHeader").html("新增评分任务");
                drawer.open({
                    direction: "right",
                    dom: ".layer-top",
                    distance: "40%"
                });
            };


            /**
             * 删除评分
             * @param obj
             */
            function delPf(obj) {
                let reqData = { id: obj.data.id };
                layer.confirm('确定删除评分任务？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    postAjax('/api/pf/delete', reqData, function (response) {
                        window.refresh();
                    }, null);
                });
            }
        })
    </script>
</body>

</html>